package top.smallway.lightbandassistant_app.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONArray;

import top.smallway.lightbandassistant_app.R;

/**
 * 搜索建议词列表适配器
 * 负责显示搜索框下方的热门搜索词列表
 */
public class WordsAdapter extends RecyclerView.Adapter<WordsAdapter.MyViewHolder> {
    /**
     * 构造函数
     * @param data 搜索建议词数据
     * @param context 上下文对象
     */
    public WordsAdapter(JSONArray data, Context context) {
        this.data = data;
        this.context = context;
    }

    // 搜索建议词数据
    private JSONArray data;
    // 上下文对象
    private final Context context;
    // 是否启用加载更多功能
    private boolean mIsLoadMoreEnabled = false;


    @NonNull
    /**
     * 创建ViewHolder
     * @param parent 父视图组
     * @param viewType 视图类型
     * @return 返回创建的ViewHolder
     */
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.words, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"ResourceAsColor", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.words.setText(data.getString(position));
    }


    @Override
    public int getItemCount() {
        return mIsLoadMoreEnabled ? (data == null ? 1 : data.size() + 1) : (data == null ? 0 : data.size());
    }

    /**
     * 视图持有者类
     * 用于缓存搜索建议词的视图组件
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView words;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            words = itemView.findViewById(R.id.words);

        }
    }
}
